#! /usr/bin/env ruby

# This is a helper script used together with examples/sentinel.rb
# It runs two Redis masters, two slaves for each of them, and two sentinels.
# After 30 seconds, the first master dies.
#
# You don't need to run this script yourself. Rather, use examples/sentinel.rb.

require "fileutils"

$pids = []

at_exit do
  $pids.each do |pid|
    begin
      Process.kill(:INT, pid)
    rescue Errno::ESRCH
    end
  end

  Process.waitall
end

base = File.expand_path(File.dirname(__FILE__))

# Masters
$pids << spawn("redis-server --port 6380 --loglevel warning")
$pids << spawn("redis-server --port 6381 --loglevel warning")

# Slaves of Master 1
$pids << spawn("redis-server --port 63800 --slaveof 127.0.0.1 6380 --loglevel warning")
$pids << spawn("redis-server --port 63801 --slaveof 127.0.0.1 6380 --loglevel warning")

# Slaves of Master 2
$pids << spawn("redis-server --port 63810 --slaveof 127.0.0.1 6381 --loglevel warning")
$pids << spawn("redis-server --port 63811 --slaveof 127.0.0.1 6381 --loglevel warning")

FileUtils.cp(File.join(base, "sentinel.conf"), "tmp/sentinel1.conf")
FileUtils.cp(File.join(base, "sentinel.conf"), "tmp/sentinel2.conf")

# Sentinels
$pids << spawn("redis-server tmp/sentinel1.conf --sentinel --port 26379")
$pids << spawn("redis-server tmp/sentinel2.conf --sentinel --port 26380")

sleep 30

Process.kill(:KILL, $pids[0])

Process.waitall
